package com.gzp.config;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gzp.service.UserService;

/**
 * dubbo使用配置类配置
 * 
 * @author gzp
 *
 */
@Configuration
public class DubboConfig {

	/**
	 * 声明当前提供方应用程序的名称
	 * 
	 * @return
	 */
	@Bean
	public ApplicationConfig applicationConfig() {
		return new ApplicationConfig("my-config-provider");
	}

	/**
	 * 指定注册中心地址
	 * 
	 * @return
	 */
	@Bean
	public RegistryConfig registryConfig() {
		return new RegistryConfig("zookeeper://127.0.0.1:2181");
	}

	/**
	 * 指定dubbo协议，将服务暴露在20890端口
	 * 
	 * @return
	 */
	@Bean
	public ProtocolConfig protocolConfig() {
		return new ProtocolConfig("dubbo", 20891);
	}

	/**
	 * 暴露userServiceImpl的服务
	 * 
	 * @param userService
	 * @param applicationConfig
	 * @param registryConfig
	 * @return
	 */
	@Bean
	public ServiceConfig<UserService> serviceConfigUserService(UserService userService,
			ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
		// 注入UserService服务
		ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
		serviceConfig.setInterface(UserService.class);
		serviceConfig.setRef(userService);

		// 注入应用程序名称
		serviceConfig.setApplication(applicationConfig);
		// 注入注册中心配置
		serviceConfig.setRegistry(registryConfig);
		// 暴露服务
		serviceConfig.export();
		return serviceConfig;
	}
}
